package com.msh.platform.elm.service.impl;

import com.msh.platform.bizmedia.base.exception.BusinessException;
import com.msh.platform.bizmedia.base.util.Tools;
import com.msh.platform.elm.common.constants.OrderStatus;
import com.msh.platform.elm.common.constants.PayType;
import com.msh.platform.elm.dao.BuyerOrdItemRepository;
import com.msh.platform.elm.dao.BuyerOrdRepository;
import com.msh.platform.elm.dao.ShopCartItemRepository;
import com.msh.platform.elm.dao.UserAcctRepository;
import com.msh.platform.elm.entity.BuyerOrd;
import com.msh.platform.elm.entity.BuyerOrdItem;
import com.msh.platform.elm.entity.ShopCartItem;
import com.msh.platform.elm.entity.UserAcct;
import com.msh.platform.elm.service.BuyerOrderService;
import com.msh.platform.elm.vo.PayOrderVO;
import com.msh.platform.elm.vo.SaveOrderVO;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BuyerOrderServiceImpl implements BuyerOrderService {

    @Autowired
    private ShopCartItemRepository shopCartItemRepository;
    @Autowired
    private BuyerOrdRepository buyerOrdRepository;
    @Autowired
    private BuyerOrdItemRepository buyerOrdItemRepository;
    @Autowired
    private UserAcctRepository userAcctRepository;
    
    @Transactional
    public String saveOrder(int buyerId, SaveOrderVO vo) {
    	Integer currTime = Tools.getCurrTime();
    	//保存订单
    	BuyerOrd order = new BuyerOrd();
    	order.setId(UUID.randomUUID().toString());
    	order.setBuyerId(buyerId);
    	order.setOrdNo(getOrderNoByTime());
    	order.setAmount(vo.getAmount());
    	order.setDiscount(vo.getDiscount());
    	order.setPayAcctNo(vo.getPayAcctNo());
     	order.setOrdRem(vo.getOrdRem());
    	order.setStatus(OrderStatus.dzf.getCode());  //订单初始状态为待支付
    	order.setCreateTime(currTime);
    	String orderId = buyerOrdRepository.save(order).getId();
    	
    	List<String> itemIds = vo.getItemIds();  //购物车中勾选的商品id数组
    	//保存订单所购买公众号档期
    	for(String itemId : itemIds) {
    		ShopCartItem item = shopCartItemRepository.findById(itemId)
    				.orElseThrow(() -> new BusinessException(-1,"根据商品ID没有获取到数据,id:"+itemId));
    		BuyerOrdItem ordItem = new BuyerOrdItem();
    		ordItem.setId(UUID.randomUUID().toString());
    		ordItem.setOrdId(orderId);
    		ordItem.setPubNoId(item.getPubNoId());
    		ordItem.setSpecId(item.getSpecId());
    		ordItem.setDqTime(item.getDqTime());
    		ordItem.setPrice(item.getPrice());
    		ordItem.setAmount(item.getAmount());
    		ordItem.setDeleteFlag("0");
    		ordItem.setCreateTime(currTime);
    		buyerOrdItemRepository.save(ordItem);
    	}
    	return orderId;
    }
    
    public void payOrderByBalance(PayOrderVO vo) {
		BuyerOrd order = buyerOrdRepository.findById(vo.getOrderId())
				.orElseThrow(() -> new BusinessException(-1,"根据订单ID没有获取到数据,id:"+vo.getOrderId()));
		BigDecimal amount = order.getAmount();  //订单总金额
		BigDecimal bonus = vo.getBonus();  //使用的奖励金
		PayType payType = PayType.getEnumByCode(vo.getPayType());  //支付类型枚举项
		
		UserAcct userAcct = userAcctRepository.findByUserId(order.getBuyerId());  //获取广告主的账户
		userAcct.setBonus(userAcct.getBonus().subtract(bonus));  //账户里扣除使用的奖励金
		userAcct.setBalVal(amount.subtract(bonus));  //剩余的用余额支付，账户里扣除支付的余额
		order.setPayType(PayType.balance.getCode());
		order.setStatus(OrderStatus.yfh.getCode());  //将订单状态改为“广告主已支付，待广告主提交资料”
		userAcctRepository.save(userAcct);
		buyerOrdRepository.save(order);
		
	}
    
    //生成订单号
    public static String getOrderNoByTime() {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    	String newDate = sdf.format(new Date());
    	String result="";
    	Random random = new Random();
    	for(int i=0;i<5;i++){
    		result += random.nextInt(10);
    	}
    	return newDate+result;
	}

}
